using System.IO;
using System.Linq;
using System.Web.Mvc;
using OurWedding1.IO;
using OurWedding1.Models;

namespace OurWedding1.Controllers
{
    [Authorize]
    public class GuestGroupsController : BaseController
    {
        private readonly IGuestGroupRepository _guestgroupRepository;
        private readonly IGuestSideRepository _guestSideRepository;

        // If you are using Dependency Injection, you can delete the following constructor
        public GuestGroupsController()
            : this(new GuestGroupRepository(),new GuestSideRepository())
        {
        }

        public GuestGroupsController(IGuestGroupRepository guestgroupRepository,IGuestSideRepository guestSideRepository)
        {
            _guestgroupRepository = guestgroupRepository;
            _guestSideRepository = guestSideRepository;
        }

        //
        // GET: /GuestGroups/

        public ActionResult Index()
        {
            if (!SelectedWeddingId.HasValue)
            {
                return RedirectToAction("Index", "Weddings");
            }
            return View();
        }

        public ActionResult IndexAjax(int side)
        {
            if (!SelectedWeddingId.HasValue)
            {
                return Content("");
            }
            var model = _guestgroupRepository.AllIncluding(guestgroup => guestgroup.Wedding, guestgroup => guestgroup.Guests);
            model = model.Where(m => m.WeddingId == SelectedWeddingId.Value);
            switch (side)
            {
                case 0:
                    model = model.Where(d => d.GuestSide.IsBrideSide);
                    break;
                case 1:
                    model = model.Where(d => d.GuestSide.IsGroomSide);
                    break;
                case 2:
                    model = model.Where(d => d.GuestSide.isCommonSide);
                    break;
                default:
                    model = model.Where(d => d.GuestSide.isBrideGroom);
                    break;
            }

            model = model.OrderBy(d => d.Name);

            return PartialView("_Index",model);
        }

        public ActionResult ExcelReport()
        {
            if (!SelectedWeddingId.HasValue)
            {
                return RedirectToAction("Index", "Weddings");
            }
            var memoryStream = new MemoryStream();

            var model = _guestgroupRepository.All.Where(g => g.WeddingId == SelectedWeddingId.Value).ToList();

            var renderer = new GuestGroupsExcellRenderer() { Rows = model };

            renderer.Render(memoryStream);

            return File(memoryStream.GetBuffer(), "application/vnd.ms-excel");

        }

        //
        // GET: /GuestGroups/Details/5

        public ViewResult Details(int id)
        {
            return View(_guestgroupRepository.Find(id));
        }

        //
        // GET: /GuestGroups/Create

        public ActionResult Create()
        {
            if (!SelectedWeddingId.HasValue)
            {
                return RedirectToAction("Index", "Weddings");
            }
            var model = new GuestGroup {WeddingId = SelectedWeddingId.Value};
            ViewBag.PossibleGuestSides = _guestSideRepository.All.Where(s=>!s.isBrideGroom);
            return View(model);
        }

        //
        // POST: /GuestGroups/Create

        [HttpPost]
        public ActionResult Create(GuestGroup guestgroup)
        {
            if (ModelState.IsValid)
            {
                _guestgroupRepository.InsertOrUpdate(guestgroup);
                _guestgroupRepository.Save();
                return RedirectToAction("Index");
            }
            ViewBag.PossibleGuestSides = _guestSideRepository.All.Where(s => !s.isBrideGroom);
            return View();
        }

        //
        // GET: /GuestGroups/Edit/5

        public ActionResult Edit(int id)
        {
            ViewBag.PossibleGuestSides = _guestSideRepository.All.Where(s => !s.isBrideGroom);
            return View(_guestgroupRepository.Find(id));
        }

        //
        // POST: /GuestGroups/Edit/5

        [HttpPost]
        public ActionResult Edit(GuestGroup guestgroup)
        {
            if (ModelState.IsValid)
            {
                _guestgroupRepository.InsertOrUpdate(guestgroup);
                _guestgroupRepository.Save();
                return RedirectToAction("Index");
            }
            ViewBag.PossibleGuestSides = _guestSideRepository.All.Where(s => !s.isBrideGroom);
            return View();
        }

        //
        // GET: /GuestGroups/Delete/5

        public ActionResult Delete(int id)
        {
            return View(_guestgroupRepository.Find(id));
        }

        //
        // POST: /GuestGroups/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            _guestgroupRepository.Delete(id);
            _guestgroupRepository.Save();

            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                _guestgroupRepository.Dispose();
                _guestSideRepository.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}

